package cn.javadog.ss.spring.aop;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @author 余勇
 * @date 2019年11月25日 16:21:00
 */
@Aspect
@Component
public class LogAspect {

	@Pointcut("execution(* cn.javadog.ss.spring.aop.AdvisedTargetBean.jointCut(..))")
	public void annotationPointCut(){
		// note 这个方法不会被执行
		System.out.println("签名的方法被执行了吗");
	}

	@After("annotationPointCut()")
	public void after(JoinPoint joinPoint) throws InvocationTargetException, IllegalAccessException {
		MethodSignature signature = (MethodSignature)joinPoint.getSignature();
		Method method = signature.getMethod();
		System.out.println("方法规则式拦截===>after" + method.getName());

	}

	/**
	 * 括号中可以直接写所谓的表达式
	 */
	@Before("annotationPointCut()")
	public void before(JoinPoint joinPoint) {
		MethodSignature signature = (MethodSignature)joinPoint.getSignature();
		Method method = signature.getMethod();
		System.out.println("方法规则式拦截===>before " + method.getName());
	}

}
